package org.iteam.xlz.core.service.message;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.iteam.commons.utils.DateUtil;
import org.iteam.core.utils.SqlBuilder;
import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.base.ColumnType;
import org.iteam.xlz.core.entity.messages.FeedbackType;

public class FeedbackTypeService extends BaseService<FeedbackType>{
	private FeedbackTypeService(){}
	
	private static FeedbackTypeService instance;
	
	public static FeedbackTypeService getInstance(){
		if(null==instance){
			instance=new FeedbackTypeService();
		}
		return instance;
	}
	
	public long counts(){
		try {
			return super.counts("select count(1) from feedbacktype where 1=1");
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public List<FeedbackType> query(long page,long rows){
		try {
			return super.query("select * from feedbacktype where 1=1 limit ?,?",(page-1)*rows,rows);
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<FeedbackType>();
		}
	}
	
	
	public FeedbackType findOne(String id){
		try {
			return super.show("select * from feedbacktype where 1=1 and tid=?",id);
		} catch (SQLException e) {
			e.printStackTrace();
			return new FeedbackType();
		}
	}
	
	
	public boolean create(FeedbackType model){
		SqlBuilder sql=SqlBuilder.createInsert("feedbacktype");
		sql.addField("tid",UUID.randomUUID().toString().replace("-","").toUpperCase());
		sql.addField("name",model.getName());
		sql.addField("creatTime",DateUtil.currentTime());
		sql.addField("creator",model.getCreator());
		try {
			return super.updates(sql.getSql(),sql.getParams())==1;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean update(FeedbackType model){
		SqlBuilder sql=SqlBuilder.createUpdate("feedbacktype");
		sql.addField("name",model.getName());
		sql.addWhere("tid",model.getTid());
		try {
			return super.updates(sql.getSql(),sql.getParams())==1;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public List<FeedbackType> getTypeList(){
		try {
			return super.query("select * from feedbacktype where 1=1");
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<FeedbackType>();
		}
	}
	
	public boolean delete(String id){
		try {
			return super.updates("delete from feedbacktype where tid=?",id)>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
}
